package com.example.my_demo.controller;

import com.example.my_demo.entity.User;
import com.example.my_demo.service.UserService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class MyController {
@Autowired
private UserService userService;
    @RequestMapping("index")
    public  String index()
    {
        return "index";
    }

    @RequestMapping("getUserById")
    @ResponseBody
    public String getUserById(int id)
    {
       User user= userService.getUserById(id);
        Gson gson=new Gson();
        String jsonUser=gson.toJson(user);
        System.out.println(jsonUser);
        return jsonUser;

    }

    @RequestMapping("addUser")
    @ResponseBody
    public int updateUser(User user)
    {

        int k=userService.addUser(user);

        return k;
    }

    @RequestMapping("updateUser")
    @ResponseBody
    public int addUser(User user)
    {

        int k=userService.updateUser(user);

        return k;
    }

    @RequestMapping("deleteUser")
    @ResponseBody
    public int deleteUser(int id)
    {

        int k=userService.deleteUser(id);

        return k;
    }

    @RequestMapping("getAllUser")
    @ResponseBody
    public String getAllUser()
    {
       List<User> list= userService.getAllUser();
        Gson gson=new Gson();
        String jsonList=gson.toJson(list);
        System.out.println(jsonList);
        return jsonList;

    }
}
